'use strict'

import { app, BrowserWindow, Menu, shell, ipcMain } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
let mainWindow
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    // height: 563,
    // useContentSize: true,
    // width: 1000,
    webPreferences: {
      nodeIntegration: true
    }
  })
  mainWindow.maximize()
  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})



// normal （常规菜单项）
// separator （分割线）
// submenu （子菜单）
// checkbox （复选菜单项）
// radio （单选菜单项）
 
const template = [
  {
    label: '编辑',
    submenu: [
      {
        label: '撤销',
        role: 'undo',
      },
      {
        label: '恢复',
        role: 'redo',
      },
      {
        type: 'separator',
      },
      { label: '截切',
        role: 'cut',
      },
      {
        label: '复制',
        role: 'copy',
      },
      {
        label: '黏贴',
        role: 'paste',
      },
 
      {
        label: '删除',
        role: 'delete',
      },
      {
        label: '全选',
        role: 'selectall',
      },
    ],
  },
  {
    label: '视图',
    submenu: [
      {
        label: '刷新页面',
        role: 'reload',
      },
      {
        label: '缩小',
        role: 'zoomin',
      },
      { label: '放大',
        role: 'zoomout',
      },
      { label: '重置缩放',
        role: 'resetzoom',
      },
      { label: '打开/关闭控制台',
        role: 'toggledevtools',
      },
      {
        label: '全屏',
        role: 'togglefullscreen',
      },
    ],
  },

];
const m = Menu.buildFromTemplate(template);
 
 
Menu.setApplicationMenu(m);
 
// 右键菜单
const contextMenuTemplate = [
  {
    label: '撤销',
    role: 'undo',
  },
  {
    label: '恢复',
    role: 'redo',
  },
  {
    type: 'separator',
  },
  {
    label: '截切',
    role: 'cut',
  },
  {
    label: '复制',
    role: 'copy',
  },
  {
    label: '黏贴',
    role: 'paste',
  },
  {
    type: 'separator',
  },  // 分隔线
  {
    label: '全选',
    role: 'selectall',
  },
  // Select All菜单项
];
 
const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);
 
 
// 监听右键事件
ipcMain.on('contextMenu', () => {
  contextMenu.popup(BrowserWindow.getFocusedWindow());
});


/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
